<?php 
if (!class_exists('Plugin')) {
	die('Hacking attemp!');
}

class PluginYii_ModuleYiiviewer
extends PluginYii_Inherit_ModuleViewer
{
	
	/**
	 * @return null
	 * @param string $sTemplate
	 */
	public function Display($sTemplate) {
		ob_start();
		parent::Display($sTemplate);
		$sContent = ob_get_clean();
		
		$sEnd = '';
		
		if($this->PluginYii_Yii_IsLoaded()){
			$this->Hook_Run('yii_yiiviewer_processoutput', array('output' => &$sContent));
			
			ob_start();
			$this->PluginYii_Yii_App()->end(0, false);
			$sEnd = ob_get_clean();
		}
		
		echo $sContent.$sEnd;
	}
	
	/**
	 * @return string
	 * @param $tpl
	 */
	public function YiiGetTemplatePath($tpl){
		if(is_array($tpl)){
			$path = '';
			if(isset($tpl['plugin'])){
				$path .= Plugin::GetTemplatePath($tpl['plugin']);
			}
			if(isset($tpl['action'])){
				$path .= 'actions/Action'.ucfirst($tpl['action']).'/';
			}
			$tpl_file = $path.$tpl[0];
		}else{
			$tpl_file = $tpl;
		}
		return $tpl_file;
	}
	
	/**
	 * @return string
	 * @param $tpl
	 * @param $data
	 * @param $local
	 */
	public function YiiSmartyFetch($tpl, $data = array(), $local = false){
		$tpl_file = $this->YiiGetTemplatePath($tpl);
		
		$sSecurityKey = Engine::getInstance()->Security_SetSessionKey();
		
		if($local){
			$v = $this->GetLocalViewer();
			$v->Assign('LIVESTREET_SECURITY_KEY',$sSecurityKey);
		}else{
			$v = $this;
		}
		
		$v->Assign("oConfig",Config::getInstance());
		$aPlugins=Engine::getInstance()->GetPlugins();
		$v->Assign("aPluginActive",array_fill_keys(array_keys($aPlugins),true));
		
		$this->Assign('sAction',Router::GetAction());
		$this->Assign('sEvent',Router::GetActionEvent());
		$this->Assign('aParams',Router::GetParams());
		$this->Assign('PATH_WEB_CURRENT',Router::GetPathWebCurrent());
		
		
		if(is_array($data) && $data){
			foreach($data as $var => $value){
				$v->Assign($var, $value);
			}
		}
		
		return $v->Fetch($tpl_file);
	}
	
	/**
	 * @return Smarty
	 */
	public function YiiGetSmarty(){
		return $this->oSmarty;
	}
	
	/**
	 * @return array
	 */
	public function YiiGetTplVars(){
		return $this->YiiGetSmarty()->_tpl_vars;
	}
	
	/**
	 * @return mixed
	 * @param string $name
	 */
	public function YiiGetTplVar($name){
		$vars = $this->YiiGetTplVars();
		if(isset($vars[$name])){
			return $vars[$name];
		}
	}
}

?>